<?php
/*
 * code generate a set of timesheet periods for a new year
*/
// Create connection
include("../inc_files/utils/dbconnection.php");

$year = $_POST["year"];

//get the date of the 1st of jan for selected year
$dateJan1st = new DateTime($year."-01-01");
$noOfDays = $dateJan1st->format('w');

//find the date of the Sunday of the first week
$weekStart = $dateJan1st;
$weekStart->modify("-".$noOfDays." days");

//the number of the week in the year e.g. week 52
$weekNumber = 1;


//first and last weeks may have days in previous
//or next years.  These dates in other years
//need to be flagged as 'blocked' - status of 1


//find out which days of the first week are not in the selected year
$dayStatuses = array(3,2,2,2,2,2,3); //the default state of a working week
//loop through any days that are in the previous year and flag them as blocked
if($noOfDays > 0){
	for($i=0;$i<$noOfDays;$i++){
		$dayStatuses[$i] = 1;
	}
}

//crate variables
$key = $year."w".str_pad($weekNumber, 2, "0", STR_PAD_LEFT);
$startDate = date_format($weekStart, "Y-m-d");
$closed = 0;  //timesheet open
$a = $dayStatuses[0];
$b = $dayStatuses[1];
$c = $dayStatuses[2];
$d = $dayStatuses[3];
$e = $dayStatuses[4];
$f = $dayStatuses[5];
$g = $dayStatuses[6];

$stmt = $mysqli->prepare("INSERT INTO timesheet_periods
		(PeriodID, StartDate, Closed, SunStatus,
		MonStatus, TueStatus, WedStatus,
		ThurStatus, FriStatus, SatStatus)
		VALUES (?,?,?,?,?,?,?,?,?,?);");

$stmt->bind_param('ssiiiiiiii', $key, $startDate, $closed, $a, $b, $c, $d, $e, $f, $g);

//execute the query
$stmt->execute();

/* do the first week */

/* create generic working weeek values for successive weeks until last one */
//daily statuses (working / non working)
$a = 3;
$b = 2;
$c = 2;
$d = 2;
$e = 2;
$f = 2;
$g = 3;

//move on to the next week
$weekStart->modify("+7 days");
$weekNumber++;

//loop throug the rest of the weeks until the second last one.
while ($weekStart->format('Y') <= $year) {

	//format a string for the primary key e.g. 2013w09
	$key = $year."w".str_pad($weekNumber, 2, "0", STR_PAD_LEFT);

	$startDate = date_format($weekStart, "Y-m-d");

	//execute the query
	$stmt->execute();

	//move on to the next week
	$weekStart->modify("+7 days");
	$weekNumber++;
} 


/* remove the days that fall in the next year on the final week of the year */

//find out how many days of the last week are still in this calendar year
$dateJan1st = new DateTime($year++."-01-01");
$noOfDays = $dateJan1st->format('w');
$noOfDays = 7 - $noOfDays;

//find out which days of the first week are not in the selected year
$dayStatuses = array(3,2,2,2,2,2,3);  //the default state of a working week
//loop throug any days that are in the previous year and flag them as blocked
if($noOfDays > 0){
	for($i=0;$i<$noOfDays;$i++){
		$dayStatuses[$i] = 1;
	}
}

//crate variables - in reverse order
$a = $dayStatuses[6];
$b = $dayStatuses[5];
$c = $dayStatuses[4];
$d = $dayStatuses[3];
$e = $dayStatuses[2];
$f = $dayStatuses[1];
$g = $dayStatuses[0];


$stmt = $mysqli->prepare("UPDATE timesheet_periods SET
		SunStatus=?, MonStatus=?, TueStatus=?,
		WedStatus=?, ThurStatus=?, FriStatus=?,
		SatStatus=? WHERE PeriodID = ?");

$stmt->bind_param('iiiiiiis', $a, $b, $c, $d, $e, $f, $g, $key);


//execute the query
$stmt->execute();

//tidy up database connection
$stmt->close();
$mysqli->close();
?>

